// import Vue from 'vue'
// import VueRouter from 'vue-router'
// import AboutView from '../views/AboutView.vue'

// Vue.use(VueRouter)

// const routes = [
//   {
//     path: '/',
//     name: 'home',
//     component: AboutView
//   },
//   {
//     path: '/about',
//     name: 'about',
//     component: () => import('../views/HomeView.vue')
//   },
//   {
//     path: '/wuliu',
//     name: 'wuliu',
//     component: () => import('../views/wuliu.vue')
//   },
//   {
//     path: '/image',
//     name: 'image',
//     component: () => import('../views/Image.vue')
//   },

// ]

// const router = new VueRouter({
//   mode: 'history',
//   base: process.env.BASE_URL,
//   routes
// })

// export default router

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Login from '@/components/Login'
import App2 from '../views/App2.vue'
import Image from "../views/Image.vue"
import wuliu from "../views/wuliu.vue"
import Root from "../views/Root.vue"
import AboutView from "../views/AboutView.vue"


Vue.use(Router)

const router = new Router({
  mode: 'history', //去掉url中的#
  routes: [
    {
      path: '/login',
      name: 'login',
      meta: {
        title: '登录',
        requiresAuth: false, // false表示不需要登录
      },
      component: Login
    },
    {
      path: '/',
      name: 'App2',
      meta: {
        title: '首页',
        requiresAuth: true, // true表示需要登录
      },
      component: App2,
      children: [
        {
          path: '',
          component: Image,
          meta: {
            title: '图片',
            requiresAuth: true, // true表示需要登录
          },
        },
        {
          path: 'image',
          component: Image,
          meta: {
            title: '图片',
            requiresAuth: true, // true表示需要登录
          },
        },
        {
          path: 'about',
          component: Root,
          meta: {
            title: '用户管理',
            requiresAuth: true, // true表示需要登录
          },
        },
        {
          path: 'wuliu',
          component: wuliu,
          meta: {
            title: '物流管理',
            requiresAuth: true, // true表示需要登录
          },
        }
      ],
    }
  ]
})

// 路由拦截，判断是否需要登录
router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    //判断是否有标题
    document.title = to.meta.title;
  }
  // console.log("title",document.title)
  // 通过requiresAuth判断当前路由导航是否需要登录
  if (to.matched.some(record => record.meta.requiresAuth)) {
    let token = localStorage.getItem('token')
    // console.log("token",token)
    // 若需要登录访问，检查是否为登录状态
    if (!token) {
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    } else {
      next()
    }
  } else {

    next() // 确保一定要调用 next()
  }
})

export default router;